/////////////////////////////////////////////////////////////////////////////
//
// XepdtLogHandler.cpp
//
/////////////////////////////////////////////////////////////////////////////

#include "StdAfx.h"
#include "XepdtLogHandler.h"
#include "XepdtClient.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CXepdtLogHandler

/////////////////////////////////////////////////////////////////////////////
// CXepdtLogHandler constructor and destructor

CXepdtLogHandler::CXepdtLogHandler()
{
	m_pClient = NULL;
}

CXepdtLogHandler::CXepdtLogHandler( CXepdtClient* pClient )
{
	m_pClient = NULL;

	ASSERT( pClient );
	m_pClient = pClient;
}

CXepdtLogHandler::~CXepdtLogHandler()
{
	m_pClient = NULL;
}

/////////////////////////////////////////////////////////////////////////////
// CXepdtLogHandler member function implementation

void CXepdtLogHandler::handleLog( gloox::LogLevel level, gloox::LogArea area, const std::string& message )
{
	ASSERT( m_pClient != NULL );

	#ifdef _DEBUG

	std::string strLogInfo = LogInfoFormat( level, area, message );

	if ( area == gloox::LogAreaXmlIncoming )
		m_pClient->OutputLogInfo( strLogInfo, CXepdtLogConsole::TEXT_YELLOW );
	else if ( area == gloox::LogAreaXmlOutgoing )
		m_pClient->OutputLogInfo( strLogInfo, CXepdtLogConsole::TEXT_GREEN );
	#endif // _DEBUG
}

std::string CXepdtLogHandler::LogInfoFormat( gloox::LogLevel level, gloox::LogArea area, const std::string& message )
{
	std::string strLevelInfo;
	std::string strArea;

	// Decide the log level information...
	switch ( level )
	{
	case gloox::LogLevelDebug :
		strLevelInfo = "LogLevelDebug";
		break;

	case gloox::LogLevelError :
		strLevelInfo = "LogLevelError";
		break;

	case gloox::LogLevelWarning :
		strLevelInfo = "LogLevelWarning";
		break;
	}

	// Decide the log area information...
	switch ( area )
	{
	case gloox::LogAreaClassParser :
		strArea = "Log messages from Parser.";
		break;

	//case gloox::LogAreaClassConnection :
	//	strArea = _T( "Log messages from Connection." );
	//	break;

	case gloox::LogAreaClassClient :
		strArea = "Log messages from Client.";
		break;

	case gloox::LogAreaClassClientbase :
		strArea = "Log messages from ClientBase.";
		break;

	case gloox::LogAreaClassComponent :
		strArea = "Log messages from Component.";
		break;

	case gloox::LogAreaClassDns :
		strArea = "Log messages from DNS.";
		break;

	case gloox::LogAreaAllClasses :
		strArea = "All log messages from all the classes.";
		break;

	case gloox::LogAreaXmlIncoming :
		strArea = "Incoming XML.";
		break;

	case gloox::LogAreaXmlOutgoing :
		strArea = "Outgoing XML.";
		break;

	case gloox::LogAreaUser :
		strArea = "User-defined sources.";
		break;

	case gloox::LogAreaAll :
		strArea = "All log sources.";
		break;
	}

	char buffer[10240 * 2];
	sprintf_s(buffer,10240 * 2, "%s\n\tarea: %s\n\tMessage:%s", strLevelInfo.c_str(), strArea.c_str(), message.c_str() );
	std::string strLogInfo(buffer);

	return strLogInfo;
}
